public class RegionInitializeActivityController
{
     public Id RecordId
   {
       get
       {
             if(System.currentPageReference().getParameters().get('id')!=null)
               return System.currentPageReference().getParameters().get('id').substring(0,15);  
             else
                 return null;
       }
       set;
   }   
    //获取Record Type Id
    public String RecordTypeId
    {
        get{        
                return System.currentPageReference().getParameters().get('RecordType');     
           }  
        set;
    }
    public Boolean IsSelectedAllCompetition{get;set;}//全选所有的竞品信息
    public Boolean CheckParticipatorAll{get;set;}//全选参与者列表
    public CommonMethod getMethod;//公共方法
    public CommonAttribute CommonAtt{get;set;}//公共属性
    public Boolean IsNc{get;set;}//是否是NC店
    public Activity_Information__c ActivityInit{get;set;}//初始化活动基本信息
    public List<AllocationResourceEntry> ETAllocationResourceList{get;set;}//ET资源分配信息
    public List<AllocationResourceEntry> FGAllocationResourceList{get;set;}//FG资源分配信息
    public List<ActivityCompetitionBrandEntry> CompetitionList{get;set;}//竞品信息
    public List<ActivityParticipatorEntry> FGParticipatorList{get;set;}//FG区域参与列表
    public List<ActivityParticipatorEntry> ETParticipatorList{get;set;}//ET区域参与列表
    public Set<string> ETParticipatorKeyString;//保存唯一ET参与者列表
    public Set<string> FGParticipatorKeyString;//保存唯一FG参与者列表
    
    public RegionInitializeActivityController()
    {        
        CommonAtt=new CommonAttribute ();
         getMethod=new CommonMethod();
        if(FGAllocationResourceList==NULL)
            FGAllocationResourceList=new List<AllocationResourceEntry>();
        else
            FGAllocationResourceList.clear();
            
        if(ETAllocationResourceList==NULL)
            ETAllocationResourceList=new List<AllocationResourceEntry>();
        else
            ETAllocationResourceList.clear();
            
        if(CompetitionList==null)
            CompetitionList=new List<ActivityCompetitionBrandEntry>();
        else
            CompetitionList.clear();
            
        if(ETParticipatorKeyString==NULL)
            ETParticipatorKeyString=new Set<string>();
        else
            ETParticipatorKeyString.clear();
            
        if(FGParticipatorKeyString==NULL)
            FGParticipatorKeyString=new Set<string>();
        else
            FGParticipatorKeyString.clear();
            
       if(FGParticipatorList==NULL)
           FGParticipatorList=new List<ActivityParticipatorEntry>();
       else
           FGParticipatorList.clear();
        
        if(ETParticipatorList==NULL)
            ETParticipatorList=new List<ActivityParticipatorEntry>();
        else
            ETParticipatorList.clear();
            
        //更新状态
        if(RecordId!=NULL)
        {
            ActivityInit=[Select Id,Name,  Activity_End_Date__c, Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c, 
                                 Activity_Type_State__c, Activity_Type__c,Serial_Number__c FROM Activity_Information__c where id=: RecordId];
             LoadCompetitionBrandList();
              LoadParticipatorList();
         }
         else//新增活动
         {
             ActivityInit=new Activity_Information__c();
             ActivityInit.RecordTypeId=RecordTypeId;
             ActivityInit.Activity_Stage__c='草稿';
             ActivityInit.Activity_Type_State__c='正常';
         }
         GetResourceList();
        
    }
    //获得资源列表
    public void GetResourceList()
    {
        String region=getMethod.getUserRegion();
        if(RecordId==NULL)
        {
        //城市的总分配量-城市总的已使用量
        //缺少条件活动结束时间小于今天
           List<AggregateResult> rsList= [select sum(Convert_To_Quantity_Number__c) totalNumber,
                                                            sum(HasUsedResourceNumber__c) hasUsedTotalNumber,
                                                            Participator_Detail_Outlet__r.Outlet_Name__r.City__r.Name cityName,
                                                            Resource_Allocation__r.Product_Name__r.Name resourceName,
                                                            Resource_Allocation__r.Product_Name__r.SKU_Code__c,
                                                            Resource_Allocation__r.Resource_Type__c
                                                            from Resource_Allocation_Detail__c 
                                                            where WhetherAllocateOrNot__c=false
                                                            and Participator_Detail_Outlet__r.Outlet_Name__r.OutletRegion__c=:region
                                                            and Resource_Quantity_Number__c!=NULL
                                                            group by Participator_Detail_Outlet__r.Outlet_Name__r.City__r.Name,
                                                            Resource_Allocation__r.Product_Name__r.Name,
                                                            Resource_Allocation__r.Product_Name__r.SKU_Code__c,
                                                            Resource_Allocation__r.Resource_Type__c];
            
           for(AggregateResult rs:rsList)
           {
               Integer HasUsedNum=(Integer)rs.get('hasUsedTotalNumber');
               decimal TotalNumber= (decimal)rs.get('totalNumber');
               if(HasUsedNum==NULL)
                   HasUsedNum=0;
               if(TotalNumber > HasUsedNum)
               {
                   AllocationResourceEntry entry=new AllocationResourceEntry();
                   entry.CityName=(String)rs.get('cityName');
                   entry.ETCode=(String)rs.get('SKU_Code__c');
                   entry.ETName=(string)rs.get('resourceName');
                   entry.TotalNumber=Integer.valueOf(TotalNumber)-HasUsedNum;
                   if(rs.get('Resource_Type__c')=='ET')
                       ETAllocationResourceList.add(entry);
                   else
                       FGAllocationResourceList.add(entry);
               }
           }  
         }
         else//更新状态
         {
             List<Resource_Allocation__c> resList=[select CityName__c,Product_Code__c,
                                                                         Product_Name__c,
                                                                         Resource_Allocation_Number__c,
                                                                         Resource_Type__c 
                                                                         from Resource_Allocation__c where Activity_Information_Name__c=:RecordId];
               for(Resource_Allocation__c res:resList)
               {
                    AllocationResourceEntry entry=new AllocationResourceEntry();
                   entry.CityName=res.CityName__c;
                   entry.ETCode=res.Product_Code__c;
                   entry.ETName=res.Product_Name__c;
                   entry.TotalNumber=(Integer)res.Resource_Allocation_Number__c;
                    if(res.Resource_Type__c=='ET')   
                        ETAllocationResourceList.add(entry);
                    else 
                        FGAllocationResourceList.add(entry);
               }
         }
    }
    //加载竞品信息
    public void LoadCompetitionBrandList()
    {
       for(Activity_Competition_Brand_Information__c acbi: [select id,Competition_Cost_Source__c,
                                                                                         Competition_Cost_Type__c,Competition_Promotion_Type__c 
                                                                                          from  Activity_Competition_Brand_Information__c
                                                                                           where Activity_Information__c=:RecordId])
        {
            CompetitionList.add(new ActivityCompetitionBrandEntry(false,acbi));
        }
    }
    //参与列表明细
    public void LoadParticipatorList()
    {
       List<ParticipatorArea__c> areaList=[select Area__c,City__c,Channel__c,IsNCOrNot__c,KA__c,
                                                               OutletType__c,Region__c,Sub_region__c,Type__c,Outlet_Sum_Total__c 
                                                               from ParticipatorArea__c 
                                                               where Activity_Name__c=:RecordId];
        for(ParticipatorArea__c area:areaList)
        {
            if(area.Type__c=='ET')
            {
                ETParticipatorKeyString.add(area.City__c+area.Channel__c+area.OutletType__c+area.KA__c);
                ETParticipatorList.add(new ActivityParticipatorEntry(false,area));
                
             }
            else
            {
                FGParticipatorKeyString.add(area.City__c+area.Channel__c+area.OutletType__c+area.KA__c);
                FGParticipatorList.add(new ActivityParticipatorEntry(false,area));
             }
        }
    }
    
      //添加竞品信息
     public PageReference addCompetitionBrandAction()
     {
         if(CompetitionList==NULL)
             CompetitionList=new  List<ActivityCompetitionBrandEntry>();
             try
             {
                 if(CommonAtt.RegionPromotionTypeSelectedValue=='' || CommonAtt.RegionPromotionTypeSelectedValue==NULL)
                      throw new CustomException ('请选择竟品促销形式');
                 if(CommonAtt.RegionCostTypeSelectedValue=='' || CommonAtt.RegionCostTypeSelectedValue==NULL)
                      throw new CustomException ('请选择竞品费用形式');
                  if(CommonAtt.CostSourceSelectedValue=='' || CommonAtt.CostSourceSelectedValue==NULL)
                      throw new CustomException ('请选择竞品费用来源');
                      
                   for(ActivityCompetitionBrandEntry entry:CompetitionList)   
                   {
                       if(entry.ActivityCompetitionMsg.Competition_Cost_Source__c==CommonAtt.CostSourceSelectedValue &&
                          entry.ActivityCompetitionMsg.Competition_Cost_Type__c== CommonAtt.RegionCostTypeSelectedValue&&
                          entry.ActivityCompetitionMsg.Competition_Promotion_Type__c==CommonAtt.RegionPromotionTypeSelectedValue)
                              return null;
                   }
                    Activity_Competition_Brand_Information__c activity=new Activity_Competition_Brand_Information__c(Activity_Information__c=RecordId,
                                                                                                  Competition_Cost_Source__c=CommonAtt.CostSourceSelectedValue,
                                                                                                  Competition_Cost_Type__c=CommonAtt.RegionCostTypeSelectedValue,
                                                                                                   Competition_Promotion_Type__c=CommonAtt.RegionPromotionTypeSelectedValue,
                                                                                                    Type__c='OT' );
                   CompetitionList.add(new ActivityCompetitionBrandEntry(false,activity));
                   insert activity;
             }
             catch(Exception ex)
             {
                  ApexPages.addMessages(ex);
                 return null;
             }
             CommonAtt.RegionPromotionTypeSelectedValue='';
             CommonAtt.RegionCostTypeSelectedValue='';
             CommonAtt.CostSourceSelectedValue='';
             
             return null;
     }
     //删除竞品信息
      public PageReference DeleteCompetitionBrandAction()
      {
         for(Integer i=0;i<CompetitionList.size();i++)
         {
              ActivityCompetitionBrandEntry entry=CompetitionList[i];
             if(entry.IsCheck)
             {
                 CompetitionList.remove(i);
                 delete [select id from Activity_Competition_Brand_Information__c where id=:entry.ActivityCompetitionMsg.Id];
                 i--;
             }
         }
          return null;
      }
}